<template>
  <div class="container">
    <!-- 顶部工具栏 -->
    <div class="toolbar">
      <el-button type="primary" @click="toggleEditMode">{{ isEditMode ? '取消编辑' : '编辑' }}</el-button>
      <el-button type="info" @click="refreshData">刷新</el-button>
      <el-button type="success" @click="auditData">审核</el-button>
      <el-button type="warning" @click="archiveData">归档</el-button>
      <el-button type="danger" @click="deleteData">删除</el-button>
    </div>

    <!-- 主表部分 -->
    <div class="main-table" style="height: 20%; overflow: hidden;">
      <el-form :model="mainForm" ref="mainFormRef" label-width="120px">
        <el-form-item label="采购单号">
          <el-input v-model="mainForm.purchaseOrderNo" :disabled="!isEditMode" />
        </el-form-item>
        <el-form-item label="供应商名称">
          <el-input v-model="mainForm.supplierName" :disabled="!isEditMode" />
        </el-form-item>
        <!-- 其他字段 -->
      </el-form>
    </div>

    <!-- 子表1部分 -->
    <div class="sub-table1" style="height: 60%; overflow-y: scroll;">
      <el-button type="primary" @click="addRowToSubTable1">插入行</el-button>
      <el-button type="info" @click="copyRowToSubTable1">复制行</el-button>
      <el-button type="danger" @click="deleteRowFromSubTable1">删除行</el-button>
      <el-button type="warning" @click="selectBOM">选择BOM</el-button>
      <el-button type="info" @click="printLabel">打印标签</el-button>
      <el-button type="success" @click="exportTemplate">导出模板</el-button>

      <el-table :data="subTable1Data" border>
        <el-table-column label="选择" type="selection" width="55" />
        <el-table-column prop="itemName" label="物料名称" />
        <el-table-column prop="quantity" label="数量" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button @click="removeRowFromSubTable1(scope.$index)" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 子表2、子表3、子表4部分，使用 el-tabs 切换 -->
    <div class="sub-table2-3-4" style="height: 20%; overflow-y: scroll;">
      <el-tabs v-model="activeTab" type="card">
        <el-tab-pane label="子表2" name="subTable2">
          <el-button type="primary" @click="addRowToSubTable2">新增行</el-button>
          <el-button type="danger" @click="deleteRowFromSubTable2">删除行</el-button>
          <el-button type="info" @click="calculateMaterial">计算用料</el-button>
          <el-button type="info" @click="presetAdd">按预设增加</el-button>
          <el-button type="info" @click="selectMaterial">选料</el-button>
          <el-button type="info" @click="copyRowToSubTable2">复制行</el-button>

          <el-table :data="subTable2Data" border>
            <el-table-column label="选择" type="selection" width="55" />
            <el-table-column label="内容" />
            <el-table-column label="操作">
              <template #default="scope">
                <el-button @click="removeRowFromSubTable(scope.$index, 0)" type="danger">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

        <el-tab-pane label="子表3" name="subTable3">
          <el-button type="primary" @click="addRowToSubTable3">新增行</el-button>
          <el-button type="danger" @click="deleteRowFromSubTable3">删除行</el-button>
          <el-button type="info" @click="calculateMaterial">计算用料</el-button>
          <el-button type="info" @click="presetAdd">按预设增加</el-button>
          <el-button type="info" @click="selectMaterial">选料</el-button>
          <el-button type="info" @click="copyRowToSubTable3">复制行</el-button>

          <el-table :data="subTable3Data" border>
            <el-table-column label="选择" type="selection" width="55" />
            <el-table-column label="内容" />
            <el-table-column label="操作">
              <template #default="scope">
                <el-button @click="removeRowFromSubTable(scope.$index, 1)" type="danger">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

        <el-tab-pane label="子表4" name="subTable4">
          <el-button type="primary" @click="addRowToSubTable4">新增行</el-button>
          <el-button type="danger" @click="deleteRowFromSubTable4">删除行</el-button>
          <el-button type="info" @click="calculateMaterial">计算用料</el-button>
          <el-button type="info" @click="presetAdd">按预设增加</el-button>
          <el-button type="info" @click="selectMaterial">选料</el-button>
          <el-button type="info" @click="copyRowToSubTable4">复制行</el-button>

          <el-table :data="subTable4Data" border>
            <el-table-column label="选择" type="selection" width="55" />
            <el-table-column label="内容" />
            <el-table-column label="操作">
              <template #default="scope">
                <el-button @click="removeRowFromSubTable(scope.$index, 2)" type="danger">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </div>

    <!-- 操作按钮 -->
    <div class="actions">
      <el-button :loading="isSaving" @click="toggleEditMode">{{ isEditMode ? '取消编辑' : '编辑' }}</el-button>
      <el-button :disabled="!isEditMode" @click="saveData">保存</el-button>
      <el-button @click="auditData">审核</el-button>
      <el-button @click="deleteData">删除</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElTable, ElTableColumn, ElButton, ElForm, ElFormItem, ElInput, ElTabs, ElTabPane } from 'element-plus';

const isEditMode = ref(false);
const isSaving = ref(false);
const activeTab = ref('subTable2'); // 默认显示子表2
const mainForm = ref({
  purchaseOrderNo: '',
  supplierName: ''
  // 其他字段...
});
const subTable1Data = ref([
  { itemName: '', quantity: 0 }
]);

const subTable2Data = ref([]);
const subTable3Data = ref([]);
const subTable4Data = ref([]);

const toggleEditMode = () => {
  isEditMode.value = !isEditMode.value;
};

const saveData = () => {
  if (!isEditMode.value) {
    alert("必须先进入编辑模式才能保存!");
    return;
  }
  isSaving.value = true;
  // 调用后端API保存数据
  // 示例: saveDataToBackend(mainForm.value, subTable1Data.value)
  setTimeout(() => {
    isSaving.value = false;
    alert("保存成功!");
  }, 1000);
};

const auditData = () => {
  if (!mainForm.value.purchaseOrderNo) {
    alert("请填写采购单号！");
    return;
  }
  // 调用审核接口
  alert("审核通过！");
};

const deleteData = () => {
  if (!mainForm.value.purchaseOrderNo) {
    alert("请填写采购单号！");
    return;
  }
  // 调用删除接口
  alert("删除成功！");
};

// 子表操作
const addRowToSubTable1 = () => {
  subTable1Data.value.push({ itemName: '', quantity: 0 });
};

const copyRowToSubTable1 = () => {
  const newRow = { ...subTable1Data.value[0] };
  subTable1Data.value.push(newRow);
};

const removeRowFromSubTable1 = (index) => {
  subTable1Data.value.splice(index, 1);
};

const selectBOM = () => {
  alert("选择BOM");
};

const printLabel = () => {
  alert("打印标签");
};

const exportTemplate = () => {
  alert("导出模板");
};

// 子表2,3,4的操作
const addRowToSubTable2 = () => {
  subTable2Data.value.push({});
};

const addRowToSubTable3 = () => {
  subTable3Data.value.push({});
};

const addRowToSubTable4 = () => {
  subTable4Data.value.push({});
};

const removeRowFromSubTable = (index, tableIndex) => {
  const subTableData = [subTable2Data, subTable3Data, subTable4Data];
  subTableData[tableIndex].value.splice(index, 1);
};

const calculateMaterial = () => {
  alert("计算用料");
};

const presetAdd = () => {
  alert("按预设增加");
};

const selectMaterial = () => {
  alert("选料");
};

const copyRowToSubTable2 = () => {
  const newRow = { ...subTable2Data.value[0] };
  subTable2Data.value.push(newRow);
};

const copyRowToSubTable3 = () => {
  const newRow = { ...subTable3Data.value[0] };
  subTable3Data.value.push(newRow);
};

const copyRowToSubTable4 = () => {
  const newRow = { ...subTable4Data.value[0] };
  subTable4Data.value.push(newRow);
};
</script>

<style scoped>
.actions {
  margin-top: 20px;
}

.el-table {
  margin-top: 20px;
}

.toolbar {
  margin-bottom: 20px;
}

.container {
  width: 100%;
  overflow: hidden;
}

.main-table,
.sub-table1,
.sub-table2-3-4 {
  max-height: 500px;
}

.el-tabs__content {
  overflow-y: auto;
}

.el-table th,
.el-table td {
  font-size: 12px; /* Smaller font size */
}
</style>
